package com.yc.baseapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.*;

public class TestJson {

	public void testFastJson1() {
		JSONObject bizContentJson = JSONObject.parseObject("{}");
		System.out.println("-----------------" + bizContentJson.size());
	}

	//顺序序列化json[用于生成加密字符串]
	public void testFastJson2() {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("a","1");
		jsonObject.put("c","3");
		jsonObject.put("d","4");
		jsonObject.put("e","5");
		jsonObject.put("sign","xxxx");
		jsonObject.put("b","2");
		jsonObject.put("ac","6");
		System.out.println(JSONObject.toJSONString(jsonObject, SerializerFeature.SortField.MapSortField));

	}

	//json 生产 键值对排序字符串[用于生成加密字符串]
	public void m0() {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("a","1");
		jsonObject.put("c","3");
		jsonObject.put("d","4");
		jsonObject.put("e","5");
		jsonObject.put("sign","xxxx");
		jsonObject.put("b","2");
		jsonObject.put("ac","6");

		System.out.println(json2SignStr(jsonObject));
	}

	private String json2SignStr(JSONObject json) {
		if (json == null || json.isEmpty()) {
			return null;
		}
		JSONObject.toJSONString(json, SerializerFeature.SortField.MapSortField);

		ArrayList<String> keyList = new ArrayList<>(json.keySet());
		keyList.sort(Comparator.naturalOrder());

		StringBuffer sf = new StringBuffer();
		for (String key : keyList) {
			if ("sign".equals(key)) {
				continue;
			}
			sf.append(key + "=" + json.getString(key)+ "&");
		}

		return sf.substring(0, sf.length() - 1);
	}

	public void m1() {
		JSONObject cycleOptionJson = new JSONObject();
//		String weekDays = "[1,2]";
//		String weekDays = "";
		String weekDays = "[]";
		cycleOptionJson.put("aaa", JSON.parseArray(weekDays));
		System.out.println(cycleOptionJson.toJSONString());
	}

	//json排序
	public void m2() {
		String jsonStr = "{\"size\":\"7.5\",\"width\":\"M (B)\",\"aidth\":\"M (B)\"}";

		System.out.println("无序遍历结果：");
		JSONObject jsonObj = JSON.parseObject(jsonStr);
		for (Map.Entry<String, Object> entry : jsonObj.entrySet()) {
			System.out.println(entry.getKey() + ":" + entry.getValue());
		}

		System.out.println("-------------------");
		System.out.println("有序遍历结果：");
		LinkedHashMap<String, String> jsonMap = JSON.parseObject(jsonStr, new TypeReference<LinkedHashMap<String, String>>() {
		});
		for (Map.Entry<String, String> entry : jsonMap.entrySet()) {
			System.out.println(entry.getKey() + ":" + entry.getValue());
		}
	}

	//json排序
	public void m3() {
		String jsonStr = "{\"size\":\"7.5\",\"width\":\"M (B)\",\"aidth\":\"M (B)\"}";

	}


	public String coverMap2String(net.sf.json.JSONObject data) {
		// 将JSONObject信息转换成key1=value1&key2=value2的形式
		TreeMap<String, Object> tree = new TreeMap<String, Object>();
		Iterator<Map.Entry<String, Object>> it = data.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Object> en = it.next();
			if ("sign".equals(en.getKey().trim())) {
				continue;
			}
			tree.put(en.getKey(), en.getValue().toString());
		}
		it = tree.entrySet().iterator();
		StringBuffer sf = new StringBuffer();
		while (it.hasNext()) {
			Map.Entry<String, Object> en = it.next();
			sf.append(en.getKey() + "=" + en.getValue()
					+ "&");
		}
		return sf.substring(0, sf.length() - 1);
	}

	//json中放其他对象
	public void m4() {
		JSONObject jo = new JSONObject();
		jo.put("a1", "123");
		List<String> list = Arrays.asList("bb","cc","dd");
		jo.put("list", list);
		System.out.println(jo.toJSONString());
	}

	//存int 取string
	public void m5() {
		JSONObject jo = new JSONObject();
		jo.put("a1", 123);
		jo.put("a2", "1");
		System.out.println(jo.getString("a1"));
		System.out.println(jo.getInteger("a2"));
	}

	//取空值
	public void m6() {
		JSONObject jo = new JSONObject();
		System.out.println(jo.getIntValue("a"));

	}


	public static void main(String[] args) {
		TestJson tj = new TestJson();
//		tj.testFastJson1();
//		tj.testFastJson2();
//		tj.m0();
//		tj.m1();
//		tj.m2();
//		tj.m4();
//		tj.m5();
		tj.m6();
	}
}
